<template>
    <div>
        <el-table
                :data="couponList"
                style="width: 100%">
            <el-table-column type="expand">
                <template slot-scope="props">
                    <el-form label-position="left" inline class="demo-table-expand">
                        <el-form-item label="积分">
                            <span>{{ props.row.integral }}</span>
                        </el-form-item>
                        <el-form-item label="类型">
                            <span>{{ props.row.couponCategory }}</span>
                        </el-form-item>
                        <el-form-item label="持续时间">
                            <span>{{ props.row.term }}</span>
                        </el-form-item>
                        <el-form-item label="领取时间">
                            <span >{{ timestampToTime(props.row.startTime) }}</span>
                        </el-form-item>
                        <el-form-item label="结束时间">
                            <span>{{ timestampToTime(props.row.endTime) }}</span>
                        </el-form-item>

                    </el-form>
                </template>
            </el-table-column>
            <el-table-column
                    label="店铺 ID"
                    sortable
                    prop="sellerId">
            </el-table-column>
            <el-table-column
                    label="店长名字"
                    prop="sellerName">
            </el-table-column>
            <el-table-column
                    label="类型"
                    prop="couponCategory">
            </el-table-column>
            <el-table-column
                    label="满减额度"
                    prop="Amount">
            </el-table-column>
            <el-table-column
                    label="状态"
                    prop="status"
                    :filters="[{ text: '生效', value:1 }, { text: '待审核', value: 4 }, { text: '审核未通过', value: 5 }]"
                    :filter-method="filterTag"
                    filter-placement="bottom-end">
                <template slot-scope="scope">
                    <el-tag
                            :type="scope.row.status === 1 ? 'success' :scope.row.status=== 4?'warning':'danger'"
                            disable-transitions>{{scope.row.status=== 1?'生效':scope.row.status=== 4?'待审核':'审核未通过'}}</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button
                            v-if="scope.row.status == 4"
                            size="mini"
                            @click="handlePass(scope.row.id, 1)">通过</el-button>

                    <el-button
                            v-if="scope.row.status == 4"
                            size="mini"
                            type="danger"
                            @click="handleReturn(scope.row.id, 5)">打回</el-button>
                    <span v-if="scope.row.status == 1 || scope.row.status == 5  " style="color:#C2C2C2 ">无操作</span>
                   <!-- <el-button
                            v-if="scope.row.status == 1 || scope.row.status == 5  "
                            size="mini"
                            >无操作</el-button>-->
                </template>
            </el-table-column>
        </el-table>

        <el-col :span="24" class="toolbar">
            <el-pagination layout="prev, pager, next"
                           @current-change="handleCurrentChange"
                           :page-size="10"
                           :total="total"
                           style="float:right;">
            </el-pagination>
        </el-col>
    </div>

</template>

<script>
    export default {
        name: "CouponManage",
        data(){
            return {
                couponList:[],
                total:0,
                pageNum:1,
                pageSize:10
            }
        },
        methods:{
            //分页
            handleCurrentChange(pageNum){
                //拿到这个页数
                //去调用接口
                this.pageNum=pageNum;
                this.getCouponList()
            },
            getCouponList(){
                var _vm = this;
                this.service.get('/coupon/manage/coupon/getAllCoupon.do',{
                    params:{
                        pageNum:this.pageNum
                    }
                }).then(res => {
                    if(res.data.status == 0){
                        _vm.total = res.data.count;
                        res.data.data.list.forEach(function (item, index) {
                            var data = {
                                id:item.id,
                                sellerId:item.sellerId,
                                sellerName:item.sellerName,
                                couponCategory:item.couponCategory,
                                integral:item.integral,
                                Amount:item.withAmount+'-'+item.delAmount,
                                startTime:item.startTime,
                                endTime:item.endTime,
                                term:item.term+'天',
                                status:item.status
                            }
                            if(item.couponCategory ==1){
                                data.couponCategory ='店铺优惠券'
                            }
                            if(item.couponCategory ==2){
                                data.couponCategory ='购物津贴'
                            }

                            _vm.couponList.push(data)

                        })
                        console.log( _vm.couponList)
                    }

                })
            },


            clearFilter() {
                this.$refs.filterTable.clearFilter();
            },
            filterTag(value, row) {
                return row.status === value;
            },
            handlePass(id,status){
                var vm = this;
                this.$confirm('通过审核, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    vm.service.get('/coupon/manage/coupon/updateCouponStatus.do',{
                        params:{
                            couponId:id,
                            status:status
                        }
                    }).then(res => {
                        console.log(res)
                        if(res.data.status == 0){
                            vm.$message({
                                type: 'success',
                                message: '审核通过!'
                            });
                            this.$router.go(0);
                        }else{
                            vm.$message({
                                type: 'info',
                                message: res.data.msg
                            });
                        }
                    })

                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消！'
                    });
                });

            },
            handleReturn(id,status){
                var vm = this;
                this.$confirm('打回商家申请, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'error'
                }).then(() => {
                    vm.service.get('/coupon/manage/coupon/updateCouponStatus.do',{
                        params:{
                            couponId:id,
                            status:status
                        }
                    }).then(res => {
                        console.log(res)
                        if(res.data.status == 0){
                            vm.$message({
                                type: 'success',
                                message: '打回成功!'
                            });
                            this.$router.go(0);
                        }else{
                            vm.$message({
                                type: 'info',
                                message: res.data.msg
                            });
                        }
                    })

                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消！'
                    });
                });
            },





        timestampToTime(timestamp) {
            var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
            var Y = date.getFullYear() + '-';
            var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
            var D = (date.getDate() < 10 ? '0'+date.getDate() : date.getDate()) + ' ';
            var h = (date.getHours() < 10 ? '0'+date.getHours() : date.getHours()) + ':';
            var m = (date.getMinutes() < 10 ? '0'+date.getMinutes() : date.getMinutes()) + ':';
            var s = (date.getSeconds() < 10 ? '0'+date.getSeconds() : date.getSeconds());
        return Y+M+D+h+m+s;
    }
        },
        mounted() {
            this.getCouponList()
        }
    }
</script>

<style scoped>
    .demo-table-expand {
        font-size: 0;
    }
    .demo-table-expand label {
        width: 90px;
        color: #99a9bf;
    }
    .demo-table-expand .el-form-item {
        margin-right: 0;
        margin-bottom: 0;
        width: 50%;
    }
</style>